/**
 * 
 */
package gui.view;

import java.util.Enumeration;
import java.util.Vector;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 * @author Elisa Costante
 * 
 */
public class TrustSettingsTableModel extends DefaultTableModel {

	private static final long serialVersionUID = 8736776436176585468L;
	public static int WEIGHT_COLUMN = 0;
	public static int ATTRIBUTE_NAME_COLUMN = 1;
	public static int ATTRIBUTE_DETAILS_COLUMN = 2;

	public TrustSettingsTableModel() {
		super();
		setColumnIdentifiers(new String[] { "Weight", "Attribute",
				"Attributes Details" });

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.DefaultTableModel#isCellEditable(int, int)
	 */
	@Override
	public boolean isCellEditable(int row, int column) {
		if (column == ATTRIBUTE_NAME_COLUMN
				|| column == ATTRIBUTE_DETAILS_COLUMN)
			return false;
		else
			return true;
	}

	public void removeColumnAndData(JTable jTable, int columnIndex) {

		// Disable autoCreateColumnsFromModel to prevent
		// the reappearance of columns that have been removed but
		// whose data is still in the table model
		jTable.setAutoCreateColumnsFromModel(false);

		// Removes the specified column from the table and the associated
		// call data from the table model.

		TableColumn tableColumn = jTable.getColumnModel()
				.getColumn(columnIndex);
		int columnModelIndex = tableColumn.getModelIndex();
		Vector data = this.getDataVector();
		Vector columnIdentifiers = this.columnIdentifiers;

		// Remove the column from the table
		jTable.removeColumn(tableColumn);

		// Remove the column header from the table model
		columnIdentifiers.removeElementAt(columnModelIndex);

		// Remove the column data
		for (int r = 0; r < data.size(); r++) {
			Vector row = (Vector) data.get(r);
			row.removeElementAt(columnModelIndex);
		}
		this.setDataVector(data, columnIdentifiers);

		// Correct the model indices in the TableColumn objects
		// by decrementing those indices that follow the deleted column
		Enumeration enumeration = jTable.getColumnModel().getColumns();
		for (; enumeration.hasMoreElements();) {
			TableColumn c = (TableColumn) enumeration.nextElement();
			if (c.getModelIndex() >= columnModelIndex) {
				c.setModelIndex(c.getModelIndex() - 1);
			}
		}
		this.fireTableStructureChanged();
	}
}
